from tkinter import filedialog
import tkinter as tk
import os

filepath_all = None

def select_file():
    root = tk.Tk()
    root.withdraw()
    file_path = filedialog.askopenfilename()
    if file_path:
        # 规范化路径
        file_path = os.path.normpath(file_path)
        return [file_path]  # 返回单个文件路径的列表，保持与select_folder返回格式一致
    else:
        print("已取消选择")
        return "-1"

# 遍历文件夹
def select_folder(title="选择文件夹"):
    global filepath_all
    root = tk.Tk()
    root.withdraw()

    folder_path = filedialog.askdirectory(title=title)

    if folder_path:
        folder_path = os.path.normpath(folder_path)
        
        if title == "选择中文手册":
            return folder_path
        else:
            file_paths = []
            for root_dir, dirs, files in os.walk(folder_path):
                for file in files:
                    # 跳过临时文件
                    if file.startswith('~$') or file.startswith('.~'):
                        continue
                    file_path = os.path.normpath(os.path.join(root_dir, file))
                    file_paths.append(file_path)
            filepath_all = file_paths
            return file_paths
    else:
        print("已取消选择")
        return "-1"
